package seminarski.geometrija;

import java.util.Comparator;

import common.point.PointG;

class PointLComparator implements Comparator<PointG<Double>>{
	PointG<Double> najniza;
	
	public PointLComparator(PointG<Double> najniza) {
		this.najniza = najniza;
	}
	
	private double distance(PointG<Double> p1, PointG<Double> p2){
		return Math.sqrt(((p1.x() - p2.x())*(p1.x() - p2.x()) + (p1.y() - p2.y())*(p1.y() - p2.y())));
	}
	
	@Override
	public int compare(PointG<Double> p1, PointG<Double> p2) {
		double alpha1 = 0, alpha2 = 0;
		PointG<Double> p3, p4;
		double b1, c1, b2, c2;
	
		if(p1.x() < najniza.x()){
			alpha1 = Math.PI/2;
			p3 = new PointG<Double>(najniza.x(), p1.y());
		}else{
			p3 = new PointG<Double>(p1.x(), najniza.y());
		}

		b1 = distance(najniza, p1);
		c1 = distance(najniza, p3);
		double cosA = c1/b1;
		alpha1 += Math.acos(cosA);
		
		if(p2.x() < najniza.x()){
			alpha2 = Math.PI/2;
			p4 = new PointG<Double>(najniza.x(), p2.y());
		}else{
			p4 = new PointG<Double>(p2.x(), najniza.y());
		}
		b2 = distance(najniza, p2);
		c2 = distance(najniza, p4);
		double cosB = c2/b2;
		alpha2 +=  Math.acos(cosB);
	
		if(alpha1 < alpha2)
			return -1;
		else if(alpha1 > alpha2)
			return 1;
		else return p1.x() >= p2.x() ? -1 : 1;
	}
	
}
